package net.csdn.business.common.oauth2.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.csdn.business.common.oauth2.mapper.OauthCodeMapper;
import net.csdn.business.common.domain.request.oauth.NewOauthCodeDTO;
import net.csdn.business.common.domain.request.oauth.UpdateOauthCodeDTO;
import net.csdn.business.common.oauth2.model.entity.OauthCode;
import net.csdn.business.common.domain.request.oauth.OauthCodeQuery;
import net.csdn.business.common.domain.vo.oauth.OauthCodeVO;
import net.csdn.business.common.oauth2.service.IOauthCodeService;
import net.csdn.business.common.service.SnowflakeService;
import net.csdn.business.common.service.SuperServiceImpl;
import net.csdn.business.common.utils.UserInfoParser;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


/**
 * 授权码Service业务层处理
 * 
 * @author zhangyl
 * @date 2022-11-08
 */
@Service
public class OauthCodeServiceImpl extends SuperServiceImpl<OauthCodeMapper, OauthCode> implements IOauthCodeService
{
    @Autowired
    private SnowflakeService snowflakeService;

    /**
     * 分页查询授权码列表
     *
     * @param query 授权码
     * @return 授权码
     */
    @Override
    public IPage<OauthCodeVO> findPageList(OauthCodeQuery query) {
        //获得Page对象
        Page<OauthCodeVO> page = query.getPage();
        List<OauthCodeVO> pageList=baseMapper.findPageList(page,query);
        page.setRecords(pageList);
        return page;
    }

    /**
     * 根据id查询授权码
     * 
     * @param id 授权码主键
     * @return 授权码
     */
    @Override
    public OauthCodeVO selectOauthCodeById(String id)
    {
        OauthCodeVO result=null;
        LambdaQueryWrapper<OauthCode> queryWrapper = new QueryWrapper<OauthCode>().lambda();
        queryWrapper.eq(OauthCode::getObjectId, id);
        OauthCode oauthCode=baseMapper.selectOne(queryWrapper);
        if(oauthCode!=null){
            result=new OauthCodeVO();
            BeanUtils.copyProperties(oauthCode, result);
        }
        return result;
    }

    /**
     * 根据id查询授权码详细信息
     *
     * @param code 授权码
     * @return 授权码
     */
    @Override
    public OauthCodeVO selectOauthCodeByCode(String code)
    {
        OauthCodeVO result=null;
        LambdaQueryWrapper<OauthCode> queryWrapper = new QueryWrapper<OauthCode>().lambda();
        queryWrapper.eq(OauthCode::getCode, code);
        OauthCode oauthCode=baseMapper.selectOne(queryWrapper);
        if(oauthCode!=null){
            result=new OauthCodeVO();
            BeanUtils.copyProperties(oauthCode, result);
        }
        return result;
    }

    /**
     * 查询授权码列表
     * 
     * @param query 授权码
     * @return 授权码
     */
    @Override
    public List<OauthCodeVO> selectOauthCodeList(OauthCodeQuery query)
    {
        return baseMapper.selectOauthCodeList(query);
    }

    /**
     * 新增授权码
     *
     * @param newOauthCodeDTO 授权码
     * @return 结果
     */
    @Override
    public Integer insertOauthCode(NewOauthCodeDTO newOauthCodeDTO)
    {
        OauthCode oauthCode=new OauthCode();
        BeanUtils.copyProperties(newOauthCodeDTO,oauthCode);
        oauthCode.setObjectId(snowflakeService.nextNo());
        oauthCode.setCreatedBy(UserInfoParser.getUserId());
        oauthCode.setCreatedDate(new Date());
        return baseMapper.insert(oauthCode);
    }

    /**
     * 修改授权码
     *
     * @param updateOauthCodeDTO 授权码
     * @return 结果
     */
    @Override
    public Integer updateOauthCode(UpdateOauthCodeDTO updateOauthCodeDTO)
    {
        String id=updateOauthCodeDTO.getObjectId();
        LambdaUpdateWrapper<OauthCode> updateWrapper = new UpdateWrapper<OauthCode>().lambda();
        updateWrapper.eq(OauthCode::getObjectId, id);
        OauthCode oauthCode=new OauthCode();
        BeanUtils.copyProperties(updateOauthCodeDTO,oauthCode);
        oauthCode.setLastModifiedBy(UserInfoParser.getUserId());
        oauthCode.setLastModifiedDate(new Date());
        return baseMapper.update(oauthCode,updateWrapper);
    }

    /**
     * 删除授权码信息
     * 
     * @param id 授权码主键
     * @return 结果
     */
    @Override
    public Integer deleteOauthCodeById(String id)
    {
        LambdaQueryWrapper<OauthCode> queryWrapper = new QueryWrapper<OauthCode>().lambda();
        queryWrapper.eq(OauthCode::getObjectId, id);
        return baseMapper.delete(queryWrapper);
    }

    /**
     * 删除授权码信息
     *
     * @param code 授权码
     * @return 结果
     */
    @Override
    public Integer deleteOauthCodeByCode(String code)
    {
        LambdaQueryWrapper<OauthCode> queryWrapper = new QueryWrapper<OauthCode>().lambda();
        queryWrapper.eq(OauthCode::getCode, code);
        return baseMapper.delete(queryWrapper);
    }
}
